package com.gaotu.demo.transaction.daosupport;

import com.gaotu.demo.transaction.User;
import com.gaotu.demo.transaction.UserRepository;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MybatisBasedDaoSupportUserRepository
        extends SqlSessionDaoSupport
        implements UserRepository {

    public MybatisBasedDaoSupportUserRepository(SqlSessionFactory sqlSessionFactory){
        this.setSqlSessionFactory(sqlSessionFactory);
    }

    @Override
    public void save(User user) {
        this.getSqlSessionTemplate().insert("com.gaotu.demo.transaction.insert", user);
    }

    @Override
    public User getByUserName(String name) {
        Map<String, Object> params = new HashMap<>();
        params.put("user_name", name);
        List<User> users = getSqlSessionTemplate().selectList("com.gaotu.demo.transaction.getByUserName", params);
        return users.size() > 0 ? users.get(0) : null;
    }
}
